<?php namespace App\Models\Cxhshop;

use App\Models\Base\CxhshopBase;

use Illuminate\Support\Facades\DB;

class Dispatch extends CxhshopBase { 
    public $timestamps = false;//这里表示的是不添加时间
    //配送的设置情况 
    protected $table = 'ims_ewei_shop_dispatch'; 



    public function getDefaultDispatch(){

        $query = self::query(); 
        return $query->where('isdefault','=',1)->where('enabled','=',1)->first();
       
    }


    public function getOneDispatch($id){
        $query = self::query();
        return $query->where('enabled','=',1)->where('id','=',$id)->first();
    }

    
    /**
     * 计算运费
     * @param type $param 重量或者是数量
     * @param type $d
     * @param type $calculatetype -1默认读取$d中的calculatetype值 1按数量计算运费 0按重量计算运费
     */
	public function getDispatchPrice($param, $d, $calculatetype = -1)
	{
		if (empty($d)) {
			return 0;
		}

		$price = 0;

		if ($calculatetype == -1) {
			$calculatetype = $d['calculatetype'];
		}

		if ($calculatetype == 1) {
			if ($param <= $d['firstnum']) {
				$price = floatval($d['firstnumprice']);
			}
			else {
				$price = floatval($d['firstnumprice']);
				$secondweight = $param - floatval($d['firstnum']);
				$dsecondweight = (floatval($d['secondnum']) <= 0 ? 1 : floatval($d['secondnum']));
				$secondprice = 0;

				if (($secondweight % $dsecondweight) == 0) {
					$secondprice = ($secondweight / $dsecondweight) * floatval($d['secondnumprice']);
				}
				else {
					$secondprice = ((int) ($secondweight / $dsecondweight) + 1) * floatval($d['secondnumprice']);
				}

				$price += $secondprice;
			}
		}
		else if ($param <= $d['firstweight']) {
			if (0 <= $param) {
				$price = floatval($d['firstprice']);
			}
			else {
				$price = 0;
			}
		}
		else {
			$price = floatval($d['firstprice']);
			$secondweight = $param - floatval($d['firstweight']);
			$dsecondweight = (floatval($d['secondweight']) <= 0 ? 1 : floatval($d['secondweight']));
			$secondprice = 0;

			if (($secondweight % $dsecondweight) == 0) {
				$secondprice = ($secondweight / $dsecondweight) * floatval($d['secondprice']);
			}
			else {
				$secondprice = ((int) ($secondweight / $dsecondweight) + 1) * floatval($d['secondprice']);
			}

			$price += $secondprice;
		}

		return $price;
	}
    //订单详情页面
    public function orderDetail($ordersn){  
        $query = self::query();
        $field = ['ordersn','price','goodsprice as price','status','dispatchprice','createtime','paytime','refundid','id','addressid','can_mix','point'];
        $goodsfield = ['a.title','a.thumb','b.total','b.price as goodsprice'];
        $addressfield = ['realname','mobile','province','city','area','address'];
        $refundid = ['status','rtype'];
        $query = $query ->where('ordersn','=',$ordersn)->get($field);
        $count =  $query->count();
       
        if($count>0){
            $data = $query->toArray();
            foreach($data as $key => &$v){
                $v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
                if($v['paytime']){
                    $v['paytime'] = date('Y-m-d H:i:s',$v['paytime']);
                }
                $v['goods'] =DB::connection('jmft_cxh')->table('ims_ewei_shop_order_goods as b')
                ->join('ims_ewei_shop_goods as a', 'a.id', '=', 'b.goodsid')
                ->where('b.orderid','=',$v['id'])->get($goodsfield);
                $v['address'] = DB::connection('jmft_cxh')->table('ims_ewei_shop_member_address')
                ->where('id','=',$v['addressid'])->get($addressfield);
                if($v['refundid']>0){
                    $v['refund'] =DB::connection('jmft_cxh')->table('ims_ewei_shop_order_refund')->where('id','=',$v['refundid'])->get($refundid);
                }
                 
                
            }
            return $data;
        }else{
            return false;
        }
      
    }
  
    //订单退换货详情页面
    public function getRefund($refundid){
        $field = ['id','reason','createtime','rtype','images','content','paytype','applyprice','point_type','ac_point'];
        $query  =  DB::connection('jmft_cxh')->table('ims_ewei_shop_order_refund')->where('id',$refundid)->where('status',0)->get($field);
        $count = $query->count();
        if($count>0){
            $data =  $query->toArray();
            return $data;
        }else{
            return false;
        }

    }
}